using System;
using System.Collections;
using System.Reflection;
using DataBroker.Dao;

namespace FirebirdDAO
{
    ///<summary>
    /// DAO Class for the Employee table.
    ///</summary>
    public class Employee : Entity
    {
        private Int16 _EMP_NO;
        private String _FIRST_NAME;
        private String _LAST_NAME;
        private String _PHONE_EXT;
        private DateTime _HIRE_DATE;
        private String _DEPT_NO;
        private String _JOB_CODE;
        private Int16 _JOB_GRADE;
        private String _JOB_COUNTRY;
        private Decimal _SALARY;
        private String _FULL_NAME;

        ///Default constructor
        public Employee()
        {

        }

        /// <summary>
        /// Constructor for joins. 
        /// Available CHILD tables to join with:
        ///    - DepartmentTable ( Department_integ_31 )
        ///    - Project ( Project_integ_36 )
        ///    - Sales ( Sales_integ_78 )
        ///    - Employee_project ( Employee_project_integ_40 )
        ///    - Salary_history ( Salary_history_integ_56 )
        /// Available PARENT tables to join with:
        ///    - DepartmentTable
        ///    - Job
        /// </summary>
        public Employee(bool Flat, params Entity[] entities)
            : this()
        {
            foreach (Entity entity in entities)
            {
                string[] entityParts = entity.GetType().ToString().Split('.');
                string entityName = entityParts[entityParts.Length - 1];
                PropertyInfo pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this, new EntityCollection(entity), null);
                }
                entityName = entityName + "_";
                pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this, entity, null);
                }
            }
            SetFlat(Flat);
        }

        ///Constructor for Joins with flat hierarchy
        public Employee(params Entity[] entities)
            : this(true, entities)
        {
        }

        ///<summary>
        /// Property for column Emp_no.
        ///</summary>
        [IsNotNullable]
        [IsPK]
        public Int16 Emp_no
        {
            get
            {
                return _EMP_NO;
            }
            set
            {
                _EMP_NO = value;
            }
        }
        ///<summary>
        /// Property for column First_name.
        ///</summary>
        [IsNotNullable]
        [MaxLength(15)]
        public String First_name
        {
            get
            {
                return _FIRST_NAME;
            }
            set
            {
                _FIRST_NAME = value;
            }
        }
        ///<summary>
        /// Property for column Last_name.
        ///</summary>
        [IsNotNullable]
        [MaxLength(20)]
        public String Last_name
        {
            get
            {
                return _LAST_NAME;
            }
            set
            {
                _LAST_NAME = value;
            }
        }
        ///<summary>
        /// Property for column Phone_ext.
        ///</summary>
        [MaxLength(4)]
        public String Phone_ext
        {
            get
            {
                return _PHONE_EXT;
            }
            set
            {
                _PHONE_EXT = value;
            }
        }
        ///<summary>
        /// Property for column Hire_date.
        ///</summary>
        [IsNotNullable]
        public DateTime Hire_date
        {
            get
            {
                return _HIRE_DATE;
            }
            set
            {
                _HIRE_DATE = value;
            }
        }
        ///<summary>
        /// Property for column Dept_no.
        ///</summary>
        [IsNotNullable]
        [MaxLength(3)]
        public String Dept_no
        {
            get
            {
                return _DEPT_NO;
            }
            set
            {
                _DEPT_NO = value;
            }
        }
        ///<summary>
        /// Property for column Job_code.
        ///</summary>
        [IsNotNullable]
        [MaxLength(5)]
        public String Job_code
        {
            get
            {
                return _JOB_CODE;
            }
            set
            {
                _JOB_CODE = value;
            }
        }
        ///<summary>
        /// Property for column Job_grade.
        ///</summary>
        [IsNotNullable]
        public Int16 Job_grade
        {
            get
            {
                return _JOB_GRADE;
            }
            set
            {
                _JOB_GRADE = value;
            }
        }
        ///<summary>
        /// Property for column Job_country.
        ///</summary>
        [IsNotNullable]
        [MaxLength(15)]
        public String Job_country
        {
            get
            {
                return _JOB_COUNTRY;
            }
            set
            {
                _JOB_COUNTRY = value;
            }
        }
        ///<summary>
        /// Property for column Salary.
        ///</summary>
        [IsNotNullable]
        public Decimal Salary
        {
            get
            {
                return _SALARY;
            }
            set
            {
                _SALARY = value;
            }
        }
        ///<summary>
        /// Property for column Full_name.
        ///</summary>
        public String Full_name
        {
            get
            {
                return _FULL_NAME;
            }
            set
            {
                _FULL_NAME = value;
            }
        }

        #region Department_parent
        private EntityCollection _Department_parent;
        ///<summary>
        /// Related table Department_parent as a collection for hierarchical retrieval.
        ///</summary>
        [NotInTable]
        [TypeContainedAttribute(typeof(Department_parent), "Department_parent")]
        public EntityCollection Department_parent
        {
            get
            {
                return _Department_parent;
            }

            set
            {
                _Department_parent = value;
            }
        }
        private Department_parent _Department_parent_;
        ///<summary>
        /// Related table Department_parent as a single object for flat retrieval.
        ///</summary>
        [NotInTable]
        public Department_parent Department_parent_
        {
            get
            {
                return _Department_parent_;
            }

            set
            {
                _Department_parent_ = value;
            }
        }
        #endregion

        #region Project
        private EntityCollection _Project;
        ///<summary>
        /// Related table Project as a collection for hierarchical retrieval.
        ///</summary>
        [NotInTable]
        [TypeContainedAttribute(typeof(Project), "Project")]
        public EntityCollection Project
        {
            get
            {
                return _Project;
            }

            set
            {
                _Project = value;
            }
        }
        private Project _Project_;
        ///<summary>
        /// Related table Project as a single object for flat retrieval.
        ///</summary>
        [NotInTable]
        public Project Project_
        {
            get
            {
                return _Project_;
            }

            set
            {
                _Project_ = value;
            }
        }
        #endregion

        #region Sales
        private EntityCollection _Sales;
        ///<summary>
        /// Related table Sales as a collection for hierarchical retrieval.
        ///</summary>
        [NotInTable]
        [TypeContainedAttribute(typeof(Sales), "Sales")]
        public EntityCollection Sales
        {
            get
            {
                return _Sales;
            }

            set
            {
                _Sales = value;
            }
        }
        private Sales _Sales_;
        ///<summary>
        /// Related table Sales as a single object for flat retrieval.
        ///</summary>
        [NotInTable]
        public Sales Sales_
        {
            get
            {
                return _Sales_;
            }

            set
            {
                _Sales_ = value;
            }
        }
        #endregion

        #region Employee_project
        private EntityCollection _Employee_project;
        ///<summary>
        /// Related table Employee_project as a collection for hierarchical retrieval.
        ///</summary>
        [NotInTable]
        [TypeContainedAttribute(typeof(Employee_project), "Employee_project")]
        public EntityCollection Employee_project
        {
            get
            {
                return _Employee_project;
            }

            set
            {
                _Employee_project = value;
            }
        }
        private Employee_project _Employee_project_;
        ///<summary>
        /// Related table Employee_project as a single object for flat retrieval.
        ///</summary>
        [NotInTable]
        public Employee_project Employee_project_
        {
            get
            {
                return _Employee_project_;
            }

            set
            {
                _Employee_project_ = value;
            }
        }
        #endregion

        #region Salary_history
        private EntityCollection _Salary_history;
        ///<summary>
        /// Related table Salary_history as a collection for hierarchical retrieval.
        ///</summary>
        [NotInTable]
        [TypeContainedAttribute(typeof(Salary_history), "Salary_history")]
        public EntityCollection Salary_history
        {
            get
            {
                return _Salary_history;
            }

            set
            {
                _Salary_history = value;
            }
        }
        private Salary_history _Salary_history_;
        ///<summary>
        /// Related table Salary_history as a single object for flat retrieval.
        ///</summary>
        [NotInTable]
        public Salary_history Salary_history_
        {
            get
            {
                return _Salary_history_;
            }

            set
            {
                _Salary_history_ = value;
            }
        }
        #endregion
        #region DepartmentTable
        private EntityCollection _Department;
        ///<summary>
        /// Related table DepartmentTable as a collection for hierarchical retrieval.
        ///</summary>
        [NotInTable]
        [TypeContainedAttribute(typeof(DepartmentTable), "Department")]
        public EntityCollection Department
        {
            get
            {
                return _Department;
            }

            set
            {
                _Department = value;
            }
        }
        private DepartmentTable _DepartmentTable;
        ///<summary>
        /// Related table DepartmentTable as a single object for flat retrieval.
        ///</summary>
        [NotInTable]
        public DepartmentTable DepartmentTable
        {
            get
            {
                return _DepartmentTable;
            }

            set
            {
                _DepartmentTable = value;
            }
        }
        #endregion
        #region Job
        private EntityCollection _Job;
        ///<summary>
        /// Related table Job as a collection for hierarchical retrieval.
        ///</summary>
        [NotInTable]
        [TypeContainedAttribute(typeof(Job), "Job")]
        public EntityCollection Job
        {
            get
            {
                return _Job;
            }

            set
            {
                _Job = value;
            }
        }
        private Job _Job_;
        ///<summary>
        /// Related table Job as a single object for flat retrieval.
        ///</summary>
        [NotInTable]
        public Job Job_
        {
            get
            {
                return _Job_;
            }

            set
            {
                _Job_ = value;
            }
        }
        #endregion
        /// <summary>
        /// Gets join for current entity and table.
        /// </summary>
        /// <param name="tableName">Table to join with entity.</param>
        /// <returns>Join definition for entity and table.</returns>
        public override DBJoin GetJoin(string tableName)
        {
            if (string.Compare(tableName, "Department", StringComparison.OrdinalIgnoreCase) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Mngr_no");
                right.Add(GetType().Name + ".Emp_no");
                return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
            }
            if (string.Compare(tableName, "Project", StringComparison.OrdinalIgnoreCase) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Team_leader");
                right.Add(GetType().Name + ".Emp_no");
                return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
            }
            if (string.Compare(tableName, "Sales", StringComparison.OrdinalIgnoreCase) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Sales_rep");
                right.Add(GetType().Name + ".Emp_no");
                return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
            }
            if (string.Compare(tableName, "Employee_project", StringComparison.OrdinalIgnoreCase) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Emp_no");
                right.Add(GetType().Name + ".Emp_no");
                return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
            }
            if (string.Compare(tableName, "Salary_history", StringComparison.OrdinalIgnoreCase) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Emp_no");
                right.Add(GetType().Name + ".Emp_no");
                return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
            }
            if (string.Compare(tableName, "Department", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Dept_no");
                right.Add(GetType().Name + ".Dept_no");
                return new DBJoin(left, right, JoinTypeEnumeration.INNER);
            }
            if (string.Compare(tableName, "Job", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Job_code");
                right.Add(GetType().Name + ".Job_code");
                left.Add(tableName + ".Job_grade");
                right.Add(GetType().Name + ".Job_grade");
                left.Add(tableName + ".Job_country");
                right.Add(GetType().Name + ".Job_country");
                return new DBJoin(left, right, JoinTypeEnumeration.INNER);
            }
            return null;
        }
    }
}


